package com.ushine.web.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.inner.DataPermissionInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.ushine.web.component.authority.handler.QueryDataPermissionHandler;
import com.ushine.web.component.mybatis.desensitization.plugin.DesensitizationPlugin;
import org.springframework.aop.interceptor.PerformanceMonitorInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import javax.annotation.Resource;


/**
 * @Author: yls
 * @Date: 2020/08/23 21:32
 * @Description: Mybatis-plus的相关配置
 * @Version 1.0
 */
@Configuration
public class MybatisConfig {

    @Resource
    QueryDataPermissionHandler queryDataPermissionHandler;

    /**
     * 功能描述:
     * 〈设置 mybatis-plus的填充字段 执行器〉
     *
     * @return : com.baomidou.mybatisplus.core.config.GlobalConfig
     * @author : yls
     * @date : 2022/3/11 15:39
     */
    @Bean(name = "permitGlobalConfig")
    public GlobalConfig globalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setMetaObjectHandler(new MybatisMetaHandler());
        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
        dbConfig.setLogicDeleteValue("now()");
        dbConfig.setLogicNotDeleteValue("'1970-01-01 00:00:00'");
        globalConfig.setDbConfig(dbConfig);
        return globalConfig;
    }


    /**
     * 功能描述:
     * 〈 设置数据脱敏拦截器 〉
     *
     * @return : com.ushine.web.component.mybatis.desensitization.plugin.DesensitizationPlugin
     * @author : yls
     * @date : 2022/02/13 20:51
     */
    @Bean(name = "permitDesensitizationPlugin")
    public DesensitizationPlugin desensitizationPlugin() {
        return new DesensitizationPlugin();
    }


    /**
     * 功能描述:
     * 〈 乐观锁插件注入 〉
     *
     * @return : com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor
     * @author : yls
     * @date : 2022/3/8 17:00
     */
    @Bean(name = "permitOptimisticLockerInnerInterceptor")
    public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() {
        return new OptimisticLockerInnerInterceptor();
    }

    /**
     * 功能描述:
     * 〈配置分页插件〉
     *
     * @return : com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor
     * @author : yls
     * @date : 2020/10/16 13:50
     */
    @Bean(name = "permitPaginationInnerInterceptor")
    public PaginationInnerInterceptor paginationInnerInterceptor() {
        return new PaginationInnerInterceptor(DbType.MYSQL);
    }


    /**
     * 功能描述:
     * 〈 自定义数据权限拦截器 〉
     *
     * @return : com.baomidou.mybatisplus.extension.plugins.inner.DataPermissionInterceptor
     * @author : yls
     * @date : 2022/2/9 15:19
     */
    @Bean(name = "permitDataPermissionInterceptor")
    public DataPermissionInterceptor dataPermissionInterceptor() {
        // 添加数据权限插件
        DataPermissionInterceptor dataPermissionInterceptor = new DataPermissionInterceptor();
//        TemplateDataPermissionHandler templateDataPermissionHandler = new TemplateDataPermissionHandler();
        // 添加自定义的数据权限处理器
        dataPermissionInterceptor.setDataPermissionHandler(queryDataPermissionHandler);
        return dataPermissionInterceptor;
    }


    /**
     * 功能描述:
     * 〈SQL执行效率插件:在dev和test环境中开启〉
     *
     * @return : com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor
     * @author : yls
     * @date : 2020/10/16 13:50
     */
    @Profile({"dev", "test"})
    @Bean
    public PerformanceMonitorInterceptor performanceMonitorInterceptor() {
        return new PerformanceMonitorInterceptor();
    }


}
